package com.ruoyi.customer.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.customer.api.domain.CustomerManagement;
import com.ruoyi.customer.api.domain.CustomerVo;
import com.ruoyi.customer.api.domain.WxLoginBody;
import com.ruoyi.customer.service.ICustomerManagementService;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户管理Controller
 * 
 * @author ruoyi
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/management")
public class CustomerManagementController extends BaseController
{
    @Autowired
    private ICustomerManagementService customerManagementService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询客户管理列表
     */
    @RequiresPermissions("customer:management:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerManagement customerManagement)
    {
        startPage();
        List<CustomerManagement> list = customerManagementService.selectCustomerManagementList(customerManagement);
        return getDataTable(list);
    }

    /**
     * 查询客户管理列表
     */
    @PostMapping("/zxkc/list")
    public TableDataInfo getCustomerList(@RequestBody CustomerVo vo) {
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<CustomerManagement> list = customerManagementService.selectCustomerManagementList(vo);
        return getDataTable(list);
    }

    /**
     * 查询客户管理列表
     */
    @RequiresPermissions("customer:management:list")
    @GetMapping("/selectUnBindCustomerList")
    public TableDataInfo selectUnBindCustomerList(CustomerManagement customerManagement)
    {
        startPage();
        List<CustomerManagement> list = customerManagementService.selectUnBindCustomerList(customerManagement);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @RequiresPermissions("customer:management:export")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerManagement customerManagement)
    {
        List<CustomerManagement> list = customerManagementService.selectCustomerManagementList(customerManagement);
        ExcelUtil<CustomerManagement> util = new ExcelUtil<CustomerManagement>(CustomerManagement.class);
        util.exportExcel(response, list, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @RequiresPermissions("customer:management:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(customerManagementService.selectCustomerManagementById(id));
    }

    /**
     * 获取客户管理详细信息
     */
    @GetMapping(value = "wx/getInfoByPhoneNumber/{phoneNumber}")
    public AjaxResult wxGetInfo(@PathVariable("phoneNumber") String phoneNumber)
    {
        return AjaxResult.success(customerManagementService.selectCustomerManagementByPhoneNumber(phoneNumber));
    }

    /**
     * 获取客户管理详细信息
     */
    @RequiresPermissions("customer:management:query")
    @GetMapping(value = "getPersonalInfo/{id}")
    public AjaxResult getPersonalInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerManagementService.getPersonalInfo(id));
    }

    /**
     * 新增客户管理
     */
    @RequiresPermissions("customer:management:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerManagement customerManagement)
    {
        return toAjax(customerManagementService.insertCustomerManagement(customerManagement));
    }

    /**
     * 修改客户管理
     */
    @RequiresPermissions("customer:management:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerManagement customerManagement)
    {
        return toAjax(customerManagementService.updateCustomerManagement(customerManagement));
    }

    /**
     * 删除客户管理
     */
    @RequiresPermissions("customer:management:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerManagementService.deleteCustomerManagementByIds(ids));
    }

    /**
     * 注册用户信息
     */
    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody CustomerManagement customerManagement)
    {
        String username = customerManagement.getAccount();
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerCustomer"))))
        {
            return R.fail("当前系统没有开启客户注册功能！");
        }
        if (!customerManagementService.checkAccountUnique(customerManagement))
        {
            return R.fail("保存客户'" + username + "'失败，注册账号已存在");
        }
        return R.ok(customerManagementService.registerCustomer(customerManagement));
    }

    /**
     * 获取当前用户信息
     */
    @InnerAuth
    @GetMapping("/info/{account}")
    public R<LoginUser> info(@PathVariable("account") String account)
    {
        CustomerManagement customerManagement = customerManagementService.selectCustomerByAccount(account);
        if (StringUtils.isNull(customerManagement))
        {
            return R.fail("用户名或密码错误");
        }
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSystemFlag(Boolean.FALSE);
        sysUserVo.setCustomerManagement(customerManagement);
        return R.ok(sysUserVo);
    }

    /**
     * 获取客户管理详细信息
     */
    @InnerAuth
    @GetMapping(value = "/getInfoByPhoneNumber/{phoneNumber}")
    public R<CustomerManagement> getInfoByPhoneNumber(@PathVariable("phoneNumber") String phoneNumber)
    {
        return R.ok(customerManagementService.selectCustomerManagementByPhoneNumber(phoneNumber));
    }
    /**
     * 获取客户管理详细信息
     */
    @InnerAuth
    @GetMapping(value = "/getInfoById/{id}")
    public R<CustomerManagement> getInfoById(@PathVariable("id") Long id)
    {
        return R.ok(customerManagementService.selectCustomerManagementById(id));
    }


    /**
     *记录用户登录IP地址和登录时间
     */
    @InnerAuth
    @PutMapping("/recordlogin")
    public R<Boolean> recordlogin(@RequestBody CustomerManagement customerManagement)
    {
        return R.ok(customerManagementService.updateCustomerProfile(customerManagement));
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        CustomerManagement user = customerManagementService.selectCustomerManagementById(SecurityUtils.getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }


    @InnerAuth
    @PostMapping("/infoByOpenId")
    public R<LoginUser> getCustomerInfoByOpenId(@RequestBody WxLoginBody wxLoginBody){
        CustomerManagement customerInfoByOpenId = customerManagementService.getCustomerInfoByOpenId(wxLoginBody);
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSystemFlag(Boolean.FALSE);
        sysUserVo.setCustomerManagement(customerInfoByOpenId);
        sysUserVo.setOpenId(wxLoginBody.getOpenId());
        return R.ok(sysUserVo);
    }

    @GetMapping("wx/setDefaultDept/{deptId}")
    public AjaxResult setDefaultDept(@PathVariable("deptId") Long deptId){
        return success(customerManagementService.setDefaultDept(deptId));
    }
}
